import {
	getMyAsk,
	getMyAnswer,
	clearUnread,
	answerList,
	getMessage_count,
	getList,
	getNotice
} from "@/static/js/message.js";
import {
	parseTimeDot
} from '@/utils/utils.js'

export default {
	data() {
		return {
			parseTimeDot,
			tabsList: [{
				id: 0,
				name: '评论',
				icon: '/static/img/message/review_icon.png',
				msgNum: 0,
				url: '/pages/message/comment/index'
			}, {
				id: 1,
				name: '点赞',
				icon: '/static/img/message/praise_icon.png',
				msgNum: 0,
				url: '/pages/message/like/index'
			}, {
				id: 2,
				name: '公告',
				icon: '/static/img/message/affiche_icon.png',
				url: '/pages/message/affiche/index',
				msgNum: 0
			}],
			informList: [{
				id: 3,
				name: '系统通知',
				icon: '/static/img/message/gg_icon.png',
				msgNum: 0,
				url: '/pages/message/sysNotify/index',
				msgTxt: '',
				time: ''
			}, {
				id: 4,
				name: '活动通知',
				icon: '/static/img/message/activity_icon.png',
				msgNum: 0,
				msgTxt: '你的会员已到账，请查看',
				url: '/pages/message/actNotify/index',
				time: ''
			}],
			askInfo: {
				name: '问答',
				icon: '/static/img/message/ask_icon.png',
				msgNum: 0,
				msgTxt: '',
				time: '',
				handler: 'toAsk'
			},

			serverInfo: {
				name: '企服',
				icon: '/static/img/message/message_icon.png',
				msgNum: 0,
				msgTxt: '',
				time: '',
				handler: 'goServerMessage'
			},
			page0: 1,
			list0: {},

			page1: 1,
			list1: [],

			page2: 1,
			list2: [],

			messageCount: {
				question: 0,
				commentandlike: 0,
				inviteanswer: 0,
				message: 0
			},

			activeStyle: {
				'font-size': '32rpx',
				'font-weight': '500',
				'color': '#1C87F3',
			},
			inactiveStyle: {
				'font-size': '32rpx',
				'font-weight': '500',
				'color': 'rgba(0,0,0,0.8)',
			},

			// 是否登录
			isLogin: false,
			GGLengthStorage: null,
			GGLength: null,
			ALength: null,
			ALengthStorage: null,
			quiz_id: '',
			requestData: {
				page: 1,
				pagesize: 10,
			},
			proceedFlag: false, //是否沟通中或待回答
			stayAsk: {}, //待沟通的问答
			normalAsk: null, //普通问答第一项
			expertAsk: null, //专家问答第一项
			systemPopupShow: false,
			userInfo: {},
			onClickItemFlag: false,
			systemPopupShow: false,
		}
	},
	onLoad(e) {},
	onHide() {

	},
	onShow() {
		this.$store.dispatch('queryConversationList')
		this.systemPopupShow = this.$store.state.inform //获取vuex中的通知设置弹窗状态
		if (uni.getStorageSync('userInfo')) {
			this.userInfo = uni.getStorageSync('userInfo')
		} else {
			this.userInfo = {}
		}
		if (uni.getStorageSync("token")) {
			this.GGLengthStorage = uni.getStorageSync('GGLength')
			this.ALengthStorage = uni.getStorageSync('HDLength')
			this.isLogin = true
			this.init()
		} else {
			this.GGLengthStorage = uni.getStorageSync('GGLength')
			this.ALengthStorage = uni.getStorageSync('HDLength')
			this.isLogin = false
			this.getGGData()
			this.getHDData()
		}


		let serverNum = this.$store.state.serverNum ? this.$store.state.serverNum : 0;
		let wdNum = this.$store.state.wdNum ? this.$store.state.wdNum : 0;
		if (serverNum + wdNum == 0) {
			uni.removeTabBarBadge({
				index: 3
			})
		}
	},
	methods: {
		init() {
			this.getMessage_count()
			this.getList()
			this.getGGData()
			this.getHDData()
			this.getAskLists()
		},

		// 清楚未读消息
		async clearUnread() {
			uni.setStorageSync('GGLength', this.GGLength.toString())
			uni.setStorageSync('HDLength', this.ALength.toString())
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: '/pages/message/index/index' // 替换成你需要刷新的页面路径
				});
				return
			}
			// #ifdef APP-PLUS
			this.$store.state.zim.clearConversationTotalUnreadMessageCount().then(res => {
				this.$store.commit('SET_SERVER_NUM', 0);
				this.$store.commit('set_wdNum', 0);
				uni.removeTabBarBadge({
					index: 3
				})
			}).catch(err => {
				console.log(err);
			})
			// #endif
			const res = await clearUnread({})
			if (res.code == 1) {
				this.getMessage_count()
				uni.reLaunch({
					url: '/pages/message/index/index' // 替换成你需要刷新的页面路径
				});
			}
		},

		// 获取问答
		async getMessage() {
			// 回答提问
			let res = await answerList({
				type: 5,
				page: this.page0,
				pagesize: 999
			})
			if (res.code = 1) {
				if (res.data.length > 0) {
					this.list0 = res.data[0]
					let id
					if (this.proceedFlag) {
						id = this.stayAsk.id
					} else {
						id = res.data[0].quiz_id
					}
					uni.navigateTo({
						url: '/quiz/chatRoom/index?id=' + id
					})
				} else {
					uni.navigateTo({
						url: '/quiz/chatRoom/index?id=' + this.stayAsk.id
					})
				}
			}
			setTimeout(() => {
				this.onClickItemFlag = false
			}, 1000)
		},

		// 获取消息通知未读数
		async getMessage_count() {
			let serverNum = this.$store.state.serverNum
			let res = await getMessage_count({})
			if (res.code == 1) {
				let message = res.data
				console.log(message);
				this.messageCount = message
				let num = Number(message.comment) + Number(message.message) + Number(message.like) +
					Number(message.wendahuifu) + Number(message.dengnilaida)
				this.$store.commit('set_wdNum', num);
				this.tabsList[0].msgNum = message.comment
				this.tabsList[1].msgNum = message.like
				this.informList[0].msgNum = message.message
				if ((num + serverNum) > 0) {
					uni.setTabBarBadge({
						index: 3,
						text: (num + serverNum) > 99 ? '99+' : '' + (num + serverNum)
					})
				} else {
					uni.removeTabBarBadge({
						index: 3
					})
				}
				if (message.dengnilaida == '0') {
					this.askInfo.msgNum = message.wendahuifu
					this.askInfo.msgTxt = '您当前有问题正在沟通中，请点击查看>>'
				} else {
					this.askInfo.msgNum = message.dengnilaida
					this.askInfo.msgTxt = '有人向您发起了提问，请点击查看>>'
				}
				//获取当前时间
				let now = new Date()
				let month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1
				let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate()
				this.askInfo.time = month + '-' + day
			}
		},

		// 获取公告数据
		async getGGData() {
			const res = await getList({
				type: 1
			})
			if (res.code == 1) {
				this.GGLength = res.data.length
				this.list1.push(...res.data)
			}
		},

		async getHDData() {
			const res = await getList(({
				type: 2
			}))

			if (res.code == 1) {
				if (res.data.length > 0) {
					this.ALength = res.data.length
					this.informList[1].msgTxt = res.data[0].title.replace(/<[^>]*>/g, "")
					this.informList[1].time = parseTimeDot(res.data[0].createtime, '{m}-{d}')
				}
			}
		},

		onClickItem(item, type) {
			if (this.onClickItemFlag) return
			if (item.url) {
				if (item.id != 2 && item.id != 4) {
					if (!uni.getStorageSync("token")) {
						this.$tools.noTokenGo()
						return
					}
				}
				setTimeout(() => {
					this.onClickItemFlag = false
				}, 1000)
				uni.navigateTo({
					url: item.url
				})

			} else {
				this[item.handler]()
			}
			this.onClickItemFlag = true
		},

		toAsk() {
			this.getMessage()
		},

		goServerMessage() {
			let userInfo = uni.getStorageSync('userInfo')
			let token = uni.getStorageSync('token')
			setTimeout(() => {
				this.onClickItemFlag = false
			}, 1000)
			if (!token) {
				this.$tools.noTokenGo()
				return
			}
			if (userInfo.is_major == 1) {
				this.$tools.msg('专业人士暂无权限，请联系客服')
				return
			}
			uni.navigateTo({
				url: '/enterpriseServer/message/index'
			})
		},

		// 获取系统通知
		async getList() {
			const res = await getNotice({
				type: 3,
				page: 1,
				pagesize: 1
			})
			if (res.code == 1) {
				this.informList[0].msgTxt = res.data.info.replace(/<[^>]*>/g, "")
				this.informList[0].time = parseTimeDot(res.data.createtime, '{m}-{d}')
			}
		},


		//问题列表
		async getAskLists() {
			this.listStatus = 'loading'
			// 普通提问
			let res = await getMyAsk(this.requestData)
			if (res.code == 1) {
				let list = res.data.list;
				if (list.length > 0) {
					this.normalAsk = list[0]
				}
			}

			//专家我的回答
			let res1 = await getMyAnswer(this.requestData)
			if (res1.code == 1) {
				let list1 = res1.data.list
				if (list1.length > 0) {
					this.expertAsk = list1[0]
				}
			}

			if (this.normalAsk !== null && this.normalAsk.status != 3) {
				this.stayAsk = this.normalAsk
				this.proceedFlag = true
			} else if (this.expertAsk !== null && this.expertAsk.status != 3) {
				this.stayAsk = this.expertAsk
				this.proceedFlag = true
			} else {
				this.proceedFlag = false
			}
		},
	},
	computed: {
		webSocketMessage() {
			return this.$store.state.webSocketMessage;
		}
	},
	watch: {
		webSocketMessage(newValue, oldValue) {
			this.init()
		}
	},
}